﻿package mortal.common.sound
{
    import com.gengine.core.*;
    import com.gengine.media.core.play.*;
    import com.gengine.media.events.*;
    import com.greensock.*;
    import flash.system.*;
    import flash.utils.*;
    import mortal.game.resource.*;

    public class SoundManager extends Object
    {
        private var _musicVolume:Number = 0.5;
        private var _effectSoundVolune:Number = 0.5;
        private var _soundLoaded:Dictionary;
        private var _soundEffectLoaded:Dictionary;
        private var _soundPlaying:Dictionary;
        private var _bgMusic:MediaPlayCore;
        private var _bgMusic2:MediaPlayCore;
        private var _fadingStart:Boolean = false;
        private var _fadingStop:Boolean = false;
        public static const BGMUSICCOMPLETE:String = "BGMUSICCOMPLETE";
        public static const BGMUSICCOMPLETE2:String = "BGMUSICCOMPLETE2";
        private static var _instance:SoundManager = null;

        public function SoundManager()
        {
            this._soundLoaded = new Dictionary();
            this._soundEffectLoaded = new Dictionary();
            this._soundPlaying = new Dictionary();
            if (_instance)
            {
                throw new Error("SoundManager 单例！");
            }
            return;
        }// end function

        public function playEffectMusic(param1:String, param2:Number) : void
        {
            if (!Capabilities.hasAudio)
            {
                return;
            }
            var _loc_3:* = param2 * this.effectSoundVolune;
            var _loc_4:* = this._soundLoaded[param1];
            if (!_loc_4)
            {
                _loc_4 = this.createMediaPlayCore(param1, true, {onMediaEvent:this.onEffectSoundEventHandler});
                if (!_loc_4)
                {
                    return;
                }
                this._soundLoaded[param1] = _loc_4;
            }
            try
            {
                _loc_4.volume = _loc_3;
                if (_loc_4.stopped)
                {
                    _loc_4.play();
                }
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function removeEffectMusic(param1:String) : void
        {
            var _loc_2:* = this._soundLoaded[param1];
            if (_loc_2 && _loc_2.playing)
            {
                _loc_2.stop();
            }
            return;
        }// end function

        public function playBgMusic(param1:String) : void
        {
            var _loc_2:MediaPlayCore = null;
            if (!Capabilities.hasAudio)
            {
                return;
            }
            if (this._bgMusic && !this._bgMusic.stopped)
            {
                this.fadeStop(this._bgMusic);
            }
            if (!this._soundLoaded[param1])
            {
                _loc_2 = this.createMediaPlayCore(param1, true, {onMediaEvent:this.onMediaEventHandler});
                if (!_loc_2)
                {
                    return;
                }
                this._soundLoaded[param1] = _loc_2;
            }
            this._bgMusic = this._soundLoaded[param1];
            this.fadeStart(this._bgMusic, this.musicVolume);
            return;
        }// end function

        public function stopBgMusic() : void
        {
            if (this._bgMusic && !this._bgMusic.stopped)
            {
                this.fadeStop(this._bgMusic);
            }
            if (this._bgMusic2 && !this._bgMusic2.stopped)
            {
                this.fadeStop(this._bgMusic2);
            }
            return;
        }// end function

        public function playBgMusic2(param1:String) : void
        {
            var _loc_2:MediaPlayCore = null;
            if (!Capabilities.hasAudio)
            {
                return;
            }
            if (this._bgMusic2 && !this._bgMusic2.stopped)
            {
                this.fadeStop(this._bgMusic2);
            }
            if (!this._soundLoaded[param1])
            {
                _loc_2 = this.createMediaPlayCore(param1, true, {onMediaEvent:this.onMediaEventHandler2});
                if (!_loc_2)
                {
                    return;
                }
                this._soundLoaded[param1] = _loc_2;
            }
            this._bgMusic2 = this._soundLoaded[param1];
            this.fadeStart(this._bgMusic2, this.musicVolume);
            return;
        }// end function

        public function stopBgMusic2() : void
        {
            if (this._bgMusic2 && !this._bgMusic2.stopped)
            {
                this.fadeStop(this._bgMusic2);
            }
            return;
        }// end function

        private function onMediaEventHandler(event:MediaPlayEvent) : void
        {
            var _loc_2:* = event.type;
            switch(_loc_2)
            {
                case MediaPlayEvent.PLAY_COMPLETE:
                {
                    GlobalDispatcher.dispatchEvent(new GlobalEvent(BGMUSICCOMPLETE));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onMediaEventHandler2(event:MediaPlayEvent) : void
        {
            var _loc_2:* = event.type;
            switch(_loc_2)
            {
                case MediaPlayEvent.PLAY_COMPLETE:
                {
                    GlobalDispatcher.dispatchEvent(new GlobalEvent(BGMUSICCOMPLETE2));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onEffectSoundEventHandler(event:MediaPlayEvent) : void
        {
            var _loc_3:MediaPlayCore = null;
            var _loc_2:* = event.type;
            switch(_loc_2)
            {
                case MediaPlayEvent.PLAY_COMPLETE:
                {
                    _loc_3 = event.target as MediaPlayCore;
                    _loc_3.play();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function getUrlByName(param1:String) : String
        {
            var _loc_2:String = null;
            if (param1)
            {
                _loc_2 = ResConfig.instance.getUrlByName(param1);
                return _loc_2;
            }
            return "";
        }// end function

        private function getNameByUrl(param1:String) : String
        {
            var _loc_2:String = null;
            if (param1)
            {
                _loc_2 = param1.substring((param1.lastIndexOf("/") + 1));
                return _loc_2;
            }
            return "";
        }// end function

        public function soundPlay(param1:String) : void
        {
            var _loc_2:* = MusicsAndSoundsConfig.instance.getSoundFileNameByType(param1);
            this.soundPlayByName(_loc_2);
            return;
        }// end function

        public function soundPlayByName(param1:String) : void
        {
            var _loc_2:MediaPlayCore = null;
            if (!Capabilities.hasAudio)
            {
                return;
            }
            if (!param1 || this.effectSoundVolune == 0)
            {
                return;
            }
            if (param1.indexOf(".") == -1)
            {
                param1 = param1 + ".mp3";
            }
            var _loc_3:* = this.getSoundPool(param1);
            if (_loc_3.length <= 0)
            {
                _loc_2 = this.createMediaPlayCore(param1, false, {onMediaEvent:this.onMediaPlayerCompleteHandler});
                if (!_loc_2)
                {
                    return;
                }
            }
            else
            {
                _loc_2 = _loc_3.pop() as MediaPlayCore;
            }
            try
            {
                _loc_2.volume = this.effectSoundVolune;
                _loc_2.play();
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        private function onMediaPlayerCompleteHandler(event:MediaPlayEvent) : void
        {
            var _loc_2:String = null;
            switch(event.type)
            {
                case MediaPlayEvent.PLAY_COMPLETE:
                {
                    _loc_2 = this.getNameByUrl((event.target as MediaPlayCore).url);
                    this.disposeSound(event.target, _loc_2);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function getSoundPool(param1:String) : Array
        {
            var _loc_2:* = this._soundEffectLoaded[param1] as Array;
            if (_loc_2)
            {
                return _loc_2;
            }
            this._soundEffectLoaded[param1] = [];
            return this._soundEffectLoaded[param1];
        }// end function

        private function disposeSound(param1, param2:String) : void
        {
            var _loc_3:* = param2.indexOf("?v=");
            if (_loc_3 != -1)
            {
                param2 = param2.substring(0, _loc_3);
            }
            var _loc_4:* = this.getSoundPool(param2);
            _loc_4.push(param1);
            return;
        }// end function

        private function getMediaType(param1:String = null) : String
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:String = null;
            if (param1)
            {
                _loc_2 = param1.split(".");
                _loc_3 = _loc_2.length;
                _loc_4 = _loc_2[(_loc_3 - 1)];
                return _loc_4.toLowerCase();
            }
            return "";
        }// end function

        private function fadeStart(param1:MediaPlayCore, param2:Number) : void
        {
            try
            {
                param1.volume = 0;
                param1.play();
                this._fadingStart = true;
                TweenLite.to(param1, 4, {volume:param2, onComplete:this.onStartCompleteHandler});
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        private function onStartCompleteHandler() : void
        {
            this._fadingStart = false;
            return;
        }// end function

        private function fadeStop(param1:MediaPlayCore) : void
        {
            var sound:* = param1;
            if (sound && sound.playing)
            {
                var onStopCompleteHandler:* = function () : void
            {
                if (!sound.stopped)
                {
                    sound.stop();
                }
                _fadingStop = false;
                return;
            }// end function
            ;
                this._fadingStop = true;
                TweenLite.to(sound, 3, {volume:0, onComplete:onStopCompleteHandler});
            }
            return;
        }// end function

        public function set musicVolume(param1:Number) : void
        {
            this._musicVolume = param1;
            if (this._bgMusic)
            {
                if (this._fadingStart)
                {
                    TweenLite.killTweensOf(this._bgMusic);
                }
                this._bgMusic.volume = param1;
            }
            if (this._bgMusic2)
            {
                if (this._fadingStart)
                {
                    TweenLite.killTweensOf(this._bgMusic2);
                }
                this._bgMusic2.volume = param1;
            }
            return;
        }// end function

        public function get musicVolume() : Number
        {
            return this._musicVolume;
        }// end function

        public function get effectSoundVolune() : Number
        {
            return this._effectSoundVolune;
        }// end function

        public function set effectSoundVolune(param1:Number) : void
        {
            this._effectSoundVolune = param1;
            return;
        }// end function

        private function createMediaPlayCore(param1:String, param2:Boolean, param3:Object) : MediaPlayCore
        {
            var _loc_4:MediaPlayCore = null;
            var _loc_5:* = this.getMediaType(param1);
            var _loc_6:* = this.getUrlByName(param1);
            if (!_loc_6)
            {
                return null;
            }
            switch(_loc_5)
            {
                case "mp3":
                {
                    _loc_4 = new MP3PlayCore(_loc_6, 1000, false, 250, param3);
                    break;
                }
                case "flv":
                case "f4v":
                case "mp4":
                case "m4a":
                case "mov":
                case "mp4v":
                case "aac":
                case "aa":
                case "3gp":
                case "3g2":
                {
                    _loc_4 = new NetStreamPlayCore(_loc_6, 1000, param2, 250, param3);
                    break;
                }
                default:
                {
                    _loc_4 = null;
                    break;
                }
            }
            return _loc_4;
        }// end function

        public static function get instance() : SoundManager
        {
            if (!_instance)
            {
                _instance = new SoundManager;
            }
            return _instance;
        }// end function

    }
}
